using System;
using UnityEngine;
using FairyGUI;
using System.Collections.Generic;

namespace ABA
{
    public class FGUIAdaptive : Singleton<FGUIAdaptive>
    {
        public int BOTTOM_OFFSET_Y = 15;
        public Dictionary<string, float> idDic = new Dictionary<string, float>();

        public void AlignTopY(GObject gObject)
        {
            float offsetY;
            // Debug.Log("AlignBottomY id ="+gObject.id);
            if (idDic.ContainsKey(gObject.id))
            {
                idDic.TryGetValue(gObject.id, out offsetY);
            }
            else
            {
                offsetY = gObject.y;
                idDic.Add(gObject.id, offsetY);
            }

            gObject.y = offsetY + GetSafaAreaOffset();
        }
        public void AlignBottomY(GObject gObject)
        {
            float offsetY;
            // Debug.Log("AlignBottomY id ="+gObject.id);
            if (idDic.ContainsKey(gObject.id))
            {
                idDic.TryGetValue(gObject.id, out offsetY);
            }
            else
            {
                offsetY = CommData.DESIGN_HEIGHT - gObject.y;
                idDic.Add(gObject.id, offsetY);
            }

            gObject.y = GRoot.inst.height - GetSafaAreaOffset() - offsetY;
        }

        public int GetSafaAreaOffset()
        {
            int safaAreaOffset = (int)((Screen.height - Screen.safeArea.yMax) / UIContentScaler.scaleFactor);
            if (safaAreaOffset != 0)
            {
                safaAreaOffset -= 35;
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                { //上移底部
                    safaAreaOffset += BOTTOM_OFFSET_Y;
                }
            }
            else if ((float)Screen.height / (float)Screen.width > 1.8)
            {//安卓9.0以下未拿到安全区域
                safaAreaOffset = 75;
            }
            return safaAreaOffset;
        }

        public int GetContentPaneYoffset()
        {
            var safaAreaOffset = GetSafaAreaOffset();
            return Application.platform == RuntimePlatform.IPhonePlayer?safaAreaOffset - BOTTOM_OFFSET_Y : safaAreaOffset;
        }

        public float GetTrueHeightForDesignHeight(float designHeight)
        {
            float d = (float)CommData.DESIGN_HEIGHT / (float)CommData.DESIGN_WIDTH;
            float t = (float)Screen.height / (float)Screen.width;
            float sh = (float)CommData.DESIGN_WIDTH / ((float)Screen.width / (float)Screen.height);  // 都是 Data.DESIGN_RESOLUTION_X 下的 Screen height
            var th = designHeight + (sh - CommData.DESIGN_HEIGHT) - FGUIAdaptive.inst.GetSafaAreaOffset();

            return th;
        }

        public bool IsPad(){
            return (float)Screen.height / (float)Screen.width <= 1.7;
        }

        public bool IsLongScreen2(){
            return (float)Screen.height / (float)Screen.width >= 2;
        }
    }
}
